package com.gitee.wsl.unit.temperature

import com.gitee.wsl.unit.BaseUnit
import com.gitee.wsl.unit.UnitType

sealed interface TemperatureUnitType: UnitType<Float> {
    //摄氏度[℃]
    data object C: TemperatureUnitType {
        override val baseUnit: Float = 1f
    }
    //华氏度[℉]
    data object F: TemperatureUnitType {
        override val baseUnit: Float = C.baseUnit *9/5+32
    }

    //开氏度[K]
    data object K: TemperatureUnitType {
        override val baseUnit: Float = C.baseUnit + 273.15f
    }

    //兰氏度[°R]
    data object R: TemperatureUnitType {
        override val baseUnit: Float = C.baseUnit * 9/5 + 491.67f
    }

}

sealed class TemperatureUnit( value:Float,type: TemperatureUnitType):
    BaseUnit<Float, TemperatureUnitType>(value, type){

    override fun toBase(): Float {
        return value * type.baseUnit
    }

    class C(value:Float):TemperatureUnit(value,TemperatureUnitType.C){
        override fun format(): String {
            return "${value}℃"
        }
    }

    class F(value:Float):TemperatureUnit(value,TemperatureUnitType.F){
        override fun format(): String {
            return "${value}℉"
        }
    }

    class K(value:Float):TemperatureUnit(value,TemperatureUnitType.K){
        override fun format(): String {
            return "${value}K"
        }
    }

    class R(value:Float):TemperatureUnit(value,TemperatureUnitType.R){
        override fun format(): String {
            return "${value}°R"
        }
    }

    override fun convertTo(desType: TemperatureUnitType):TemperatureUnit {
         return when(desType){
             is TemperatureUnitType.C -> C(toBase()/desType.baseUnit)
             is TemperatureUnitType.F -> F(toBase()/desType.baseUnit)
             is TemperatureUnitType.K -> K(toBase()/desType.baseUnit)
             is TemperatureUnitType.R -> R(toBase()/desType.baseUnit)
         }
    }
}